package com.weixing.mall.provider.controller.cms;

import com.weixing.mall.base.wrapper.Result;
import com.weixing.mall.base.wrapper.ResultUtil;
import com.weixing.mall.core.support.BaseController;
import com.weixing.mall.core.support.auth.SysAuth;
import com.weixing.mall.core.support.auth.SysInfo;
import com.weixing.mall.provider.model.dto.cms.NewsDto;
import com.weixing.mall.provider.model.query.NewsQuery;
import com.weixing.mall.provider.service.INewsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @Author wx
 * @Date 2019/7/25 18:00
 * @Version 1.0
 */
@RestController
@RequestMapping("news")
@Api(value = "公告api",tags = "公告")
public class NewsController extends BaseController {

    @Autowired
    private INewsService newsService;

    @ApiOperation(value = "加载公告")
    @GetMapping("")
    public Result list(NewsQuery newsQuery){
        return ResultUtil.success(newsService.selectOfpage(initMpPage(),newsQuery));
    }

    @ApiOperation(value = "按id加载公告")
    @GetMapping("/{id}")
    public Result get(@PathVariable long id){
        return ResultUtil.success(newsService.selectNewsEditVoById(id));
    }

    @ApiOperation(value = "发布公告")
    @PostMapping(value = "save")
    public Result save(@Validated NewsDto newsDto, @SysInfo SysAuth auth){
        newsService.save(newsDto,auth.getUid());
        return ResultUtil.success();
    }

    @ApiOperation(value = "删除公告")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "公告id",paramType = "path",dataType = "long")
    })
    @PostMapping("del/{id}")
    public Result del(@PathVariable long id){
        return newsService.removeById(id) ? ResultUtil.success() : ResultUtil.error();
    }

    @ApiOperation(value = "批量删除公告")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "arr",value = "公告id数组",paramType = "path",dataType = "int")
    })
    @PostMapping("delAll")
    public Result del(@RequestBody Long[] ids){
        List<Long> idss = Arrays.asList(ids);
        newsService.removeByIds(idss);
        return ResultUtil.success();
    }

}
